#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@author: yuejl
@application:
@contact: lewyuejian@163.com
@file: analysis.py
@time: 2021/11/21 0021 20:41
@desc: 数据解析
'''
from typing import Dict, List

from integratior_service.controllers.common.exceptions import VariablesFormatError
from integratior_service.controllers.common.models import RequestData as request_data
from integratior_service.controllers.common.continuous_load import load_file

# 解析数据源
def resolve_step_source(variables: Dict):
    test_case_list = []
    test_step_list = []
    if not isinstance(variables, Dict):
        raise VariablesFormatError(f'test data format not supported!')
    if 'url' in variables and variables.get('url') is not None:
        request_data.url = variables.get('url')
    if 'method' in variables and variables.get('method') is not None:
        request_data.method = variables.get('method')
    if 'headers' in variables:
        request_data.headers = variables.get('headers')
        request_data.set_global_vars = test_case_list.append(request_data.headers)
    if 'request_body' in variables and isinstance(variables.get('request_body'), List):

        for variable in variables.get('request_body'):

            if isinstance(variable, Dict):
                test_step_list.append(variable.get('json_data'))
    request_data.json_data = test_step_list
    return request_data





def analysis():
    data = load_file('template.yml')['GetServiceType']
    #print(data)
    print('#'*100)
    return resolve_step_source(data)


if __name__ == '__main__':
    print(analysis().url)
    print(analysis().headers)
    print(analysis().json_data)